function [sumStMat,table,xlCell]=estimaSpectrum(funcModel,parVec,solveOpt,addSol,statesReport,stIn,NPoints,...
    outPath) 
cucd=pwd; 
if nargin < 6 
    NPoints=1000; 
end 
%% 1. Assign Base 
if isfield(stIn,'frequency')==false || isempty(stIn.frequency)==true 
    stIn.frequency='Q'; 
end 
    
if strmatch(upper(stIn.frequency),'M','exact')==1 
    lowPer=18; 
    highPer=96; 
elseif strmatch(upper(stIn.frequency),'Q','exact')==1 
    lowPer=6; 
    highPer=32;
%      lowPer=1;
%      highPer=200;
else 
    lowPer=2; 
    highPer=8; 
end 
[~,stIn]=ch_field(stIn,'lowPer',lowPer); 
[~,stIn]=ch_field(stIn,'highPer',highPer); 
dispaj('Lower Periodicity=',stIn.lowPer); 
dispaj('High  Periodicity=',stIn.highPer); 
dispaj('NPoints          =',NPoints); 

[GG,RR,~,eu,SDX,ZZ,~,~,~,~,stateNames,shockNames]=feval(funcModel,parVec,solveOpt,addSol);
if ~isequal(eu,[1;1]) 
    error('Model does not have a solution') 
end 
ZNames=extractObsNames(ZZ(:,:,1),stateNames); 
%posReport=cellposition(statesReport,stateNames); 
NPages=size(GG,3); 

%sumStMat = zeros( length(ZNames)+length(statesReport),length(shockNames.short),NPages);
counterCell = cell(NPages,1);

for ii=1:NPages; 
    [~,subCell,~,sumSt]=dsge_spectrum(GG(:,:,ii),RR(:,:,ii),ZZ(:,:,ii),SDX(:,:,ii),...
        statesReport,stateNames,shockNames.short,ZNames,stIn.lowPer,stIn.highPer,NPoints);
    if ii==1 
        sumStMat=zeros([size(sumSt.mat) NPages]); 
    end
    sumStMat(:,:,ii) = sumSt.mat;
    counterCell{ii}=sprintf('Sample %1.0f',ii);
end


%% Create LaTeX/PDF Table
table.tableHeader = cell(NPages,1);
for ii=1:NPages
    table.tableHeader{ii}=['Spectral Decomposition (',num2str(stIn.highPer),',',...
                             num2str(stIn.lowPer),')   Sample ',counterCell{ii}];
end

if nargin < 8 || isempty(outPath);
    disp('Not saving spectrum tables');
    flags.notSaving=true; 
    outPath='none'; 
    cd(cucd);
    fclose('all');    
else 
    flags.notSaving=false; 
end 


options.fileName = 'SpectrumSample';
table.outPath = outPath;
options.orientation = 'landscape';
options.maxRows = 25;
options.maxCols = 12;

table.dataMat = sumStMat;
table.rowLabels = sumSt.names;
table.colLabels = shockNames.short;
table.corner=['NPoints=',num2str(NPoints)];


for k = 1:NPages   
    xlCell = crtcell(table.dataMat(:,:,k), table.rowLabels, table.colLabels, table.tableHeader{k}, 2);
    if flags.notSaving==false
    xlswrite('dsgeSpectrum.xls', xlCell, ['model' num2str(k)]);
    end 
end

if flags.notSaving==true 
    return
end 
cd(outPath);

multiPageTable(table,options);

% Write to Excel Sheet
xlswrite('Table Spectrum',xlCell);
cd(cucd);
fclose('all');

%% Compute the variance decomposition for a wide range of frequencies - Leonardo 10/8/2018
lowPer=1;
highPer=200;
% NPoints=10000;
    [~,~,~,~]=dsge_spectrum(GG(:,:,ii),RR(:,:,ii),ZZ(:,:,ii),SDX(:,:,ii),...
        statesReport,stateNames,shockNames.short,ZNames,lowPer,highPer,NPoints);
% NPoints=1000;
    

end
  
